package kt.com.bilin.coin.ui.activity

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.bilin.coin.BuildConfig
import com.bilin.coin.R
import com.bilin.coin.bean.CommonSuccessEvent
import com.kingja.loadsir.core.LoadService
import kotlinx.android.synthetic.main.activity_v2_auth.*
import kt.com.bilin.coin.api.API_SERVICE
import kt.com.bilin.coin.api.ApiObserver
import kt.com.bilin.coin.api.toData
import kt.com.bilin.coin.common.bindViewClick
import kt.com.bilin.coin.common.changeVisibility
import kt.com.bilin.coin.common.format
import kt.com.bilin.coin.common.showLoadingCallback
import kt.com.bilin.coin.helper.faceResult
import kt.com.bilin.coin.helper.loadsir.LoadSirHelper
import kt.com.bilin.coin.helper.openFace
import kt.com.bilin.coin.utils.VariableViewUtils
import kt.com.bilin.coin.viewmodel.BackgroundCommonApi
import kt.com.bilin.coin.viewmodel.UserViewModel
import org.greenrobot.eventbus.EventBus
import java.io.File

@Route(path = "/v2/safe/AuthActivity")
class AuthActivity : BaseActivity(R.layout.activity_v2_auth) {
    companion object {
        private const val TYPE_WAY_THIS = "0"
        private const val TYPE_WAY_PARENT = "1"
    }

    private val mUserViewModel: UserViewModel by viewModels()
    private var typeWay: String = TYPE_WAY_THIS
    private var mNdpy: Int = 0
    private val mLoadService: LoadService<*> by lazy { LoadSirHelper.register(this) }
    override fun initView() {
        if (BuildConfig.DEBUG) {
            etRealName.setText("周传亮")
            etIdCard.setText("513029199407024933")
        }
        bindViewClick(this, cbAmount, cbInvite, buttonSubmit)
        VariableViewUtils().apply {
            addTextView(etRealName)
            addTextView(etIdCard)
            addOnValidationInterceptListener(this@AuthActivity::validator)
            addOnValidationResultListener(buttonSubmit)
            validator()
        }
        mLoadService.showLoadingCallback()
    }

    private fun validator(view: View) = when (view) {
        etIdCard -> etIdCard.length() == 18
        else -> null
    }

    override fun onClick(v: View?) {
        when (v) {
            cbAmount -> {
                typeWay = TYPE_WAY_THIS
                cbInvite.isChecked = false
            }
            cbInvite -> {
                typeWay = TYPE_WAY_PARENT
                cbAmount.isChecked = false
            }
            buttonSubmit -> {
                buttonSubmit.enable(false)
                if (mNdpy == 1) {
                    mUserViewModel.kycCheckBefore(etIdCard.text.toString(), typeWay, ApiObserver(this, error = { buttonSubmit.enable(true) }) { openFace { buttonSubmit.enable(true) } })
                } else {
                    openFace { buttonSubmit.enable(true) }
                }
            }
        }
    }

    override fun initData() {
        BackgroundCommonApi.appConfig(lifecycleScope) {
            if (it != null) {
                mNdpy = it.ndpy
                llPay.changeVisibility = mNdpy == 1
                mLoadService.showSuccess()
            } else {
                finish()
            }
        }
        mUserViewModel.getBlq(ApiObserver(this) {
            it?.run {
                tvBalance.text = getString(R.string.text_auth_bl_balance_num, selfAmount.format())
                tvinvitees.text = getString(R.string.text_auth_bl_invitees_num, parentAmount.format())
                rlInvite.changeVisibility = open
            }
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        faceResult(requestCode, resultCode, data) {
            mUserViewModel.kycAll(etIdCard.text.toString(), etRealName.text.toString(), typeWay, File(it), ApiObserver(this, commonButton = buttonSubmit, cancelable = false) {
                EventBus.getDefault().post(CommonSuccessEvent(CommonSuccessEvent.AuthSuccess))
                finish()
            })
        }
    }
}